﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using DyUi.Base.Constants;
using DyUi.Base.Interfaces;
using DyUi.Base.Interfaces.Services;

namespace DyUi.Core
{
    public class DyUiControl : UserControl, IDyUiControl
    {
        public String DyUiName { get; set; }
        
        public DirectoryInfo Path { get; set; }

        public Object AppDataContext { get; set; }

        public IDyUiServiceLocator ServiceLocator { get; set; }

        public IFactoryService FactoryService { get; set; }

        public IMessageService MessageService { get; set; }

        public ILoggingService LoggingService { get; set; }

        public IExecutionService ExecutionService { get; set; }

        public Guid Id { get; private set; }

        public DyUiControl()
        {
            Id = Guid.NewGuid();    
        }

        /// <summary>
        /// Initializes a new instance of the AppRegion class.
        /// </summary>
        public virtual void Prepare()
        {
            if (String.IsNullOrEmpty(Name))
                Name = DyUiName;

            if (ServiceLocator != null)
            {
                FactoryService = ServiceLocator.GetService(ServiceNames.FactoryService) as IFactoryService;
                MessageService = ServiceLocator.GetService(ServiceNames.MessageService) as IMessageService;
                LoggingService = ServiceLocator.GetService(ServiceNames.LoggingService) as ILoggingService;
                ExecutionService = ServiceLocator.GetService(ServiceNames.ExecutionService) as IExecutionService;
            }

            if (ExecutionService != null) 
                ExecutionService.PrepareControlScriptObject();
        }

        public void RouteEvent(Object sender, RoutedEventArgs e)
        {
            if (ExecutionService != null)
            {
                ExecutionService.RouteEvent(sender, e);
            }
        }
    }
}
